package org.meb.conquest.ui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.codehaus.jackson.JsonProcessingException;
import org.meb.conquest.core.Cache;
import org.meb.conquest.db.model.loc.Domain;
import org.meb.conquest.rest.auth.AuthUser;
import org.meb.conquest.rest.json.DeckLoader;
import org.meb.conquest.rest.json.JsonUtils;
import org.meb.conquest.rest.json.model.JsonDomain;
import org.meb.conquest.rest.service.QueryContext;

@Named
@RequestScoped
public class BootstrapCtrl {

	@Inject
	private AuthUser authUser;

	@Inject
	private QueryContext queryContext;

	@Inject
	private LocaleCtrl localeCtrl;

	@Inject
	private DeckLoader deckLoader;

	@Inject
	private JsonStaticCache jsonStaticCache;

	@Inject
	private Cache cache;

//	public String getCardsBootstrapData() throws JsonProcessingException, IOException {
//
//		queryContext.setUserId(authUser.getUserId());
//		queryContext.setUserLanguage(localeCtrl.getLanguage());
//
//		StringBuilder builder = new StringBuilder();
//		builder.append("\n<script>");
//		builder.append("conquest.resetCards(");
//		builder.append(jsonStaticCache.getCardsJson(localeCtrl.getLanguage()));
//		builder.append(");");
//		builder.append("</script>\n");
//		return builder.toString();
//	}
	
	public String getDecksBootstrapData() throws JsonProcessingException, IOException {

		queryContext.setUserId(authUser.getUserId());
		queryContext.setUserLanguage(localeCtrl.getLanguage());

		StringBuilder builder = new StringBuilder();
		builder.append("\n<script>");
		builder.append("conquest.resetDecks(");
		builder.append(JsonUtils.write(deckLoader.loadDecks()));
		builder.append(");");
		builder.append("</script>\n");
		return builder.toString();
	}

	public String getCardsData() throws JsonProcessingException, IOException {
		queryContext.setUserId(authUser.getUserId());
		queryContext.setUserLanguage(localeCtrl.getLanguage());
		return jsonStaticCache.getCardsJson(localeCtrl.getLanguage());
	}
	
	public String getCardSetsData() throws JsonProcessingException, IOException {
		queryContext.setUserId(authUser.getUserId());
		queryContext.setUserLanguage(localeCtrl.getLanguage());
		return jsonStaticCache.getCardSetsJson(localeCtrl.getLanguage());
	}

	public String getDomainFactionData() throws JsonProcessingException, IOException {
		return getDomainData("faction");
	}

	public String getDomainTraitData() throws JsonProcessingException, IOException {
		return getDomainData("trait");
	}

	public String getDomainKeywordData() throws JsonProcessingException, IOException {
		return getDomainData("keyword");
	}

	private String getDomainData(String name) throws JsonProcessingException, IOException {
		queryContext.setUserId(authUser.getUserId());
		queryContext.setUserLanguage(localeCtrl.getLanguage());
		
		List<JsonDomain> jsonDomains = new ArrayList<>();
		List<Domain> domains = cache.loadDomains(name);
		for (Domain domain : domains) {
			JsonDomain jsonDomain = new JsonDomain();
			jsonDomain.setValue(domain.getValue());
			jsonDomain.setDescription(domain.getDescription());
			jsonDomains.add(jsonDomain);
		}

		return JsonUtils.write(jsonDomains);
	}

	public String getMessagesData() throws JsonProcessingException, IOException {

		queryContext.setUserId(authUser.getUserId());
		queryContext.setUserLanguage(localeCtrl.getLanguage());

		ResourceBundle bundle = ResourceBundle.getBundle("resources/clientMessages", localeCtrl.getLocale());
		return JsonUtils.write(bundle);
	}
}
